var app = getApp()
Page({
  data: {
    firstName: '',
    lastName: '',
    city: ['请选择城市','北京'],
    index: 0,
    address: '',
    zip: '',
    email: '',
    cellphone: '',
    wechatNumber: '',
    addressId: null
  },

  onLoad: function (options) {
    var _this = this
    var addressId = options.id
    if (addressId) {
      wx.request({
        url: 'https://cake.cindata.cn/address/getAddressById',
        data: {
          id: addressId
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        complete: function complete(res) {
          if (res.statusCode == 200) {
            var indexTemp = 0
            for (var i = 0; i < _this.data.city.length; i++) {
              if (_this.data.city[i] == res.data.city) {
                indexTemp = i;
              }
            }
            _this.setData({
              firstName: res.data.firstName,
              lastName: res.data.lastName,
              index: indexTemp,
              address: res.data.address,
              zip: res.data.zip,
              email: res.data.email,
              cellphone: res.data.cellphone,
              wechatNumber: res.data.wechatNumber,
              addressId: res.data.id
            })
          } else {
            console.log("获取收货地址详情失败，请稍后重试！");
          }
        }
      });
    }
  },
  formSubmit: function (e) {
    var warn = "";//弹框时提示的内容
    var flag = true;//判断信息输入是否完整
    console.log(e.detail.value.city);
    if (e.detail.value.firstName.trim() == "") {
      warn = "请输入您的姓氏";
    } else if (e.detail.value.lastName.trim() == "") {
      warn = "请填写您的名字";
    } else if (e.detail.value.city.trim() == "" || e.detail.value.city.trim() == "请选择城市") {
      warn = "请输入城市";
    } else if (e.detail.value.address.trim() == "") {
      warn = "请输入地址"
    } else if (e.detail.value.cellphone.trim() == "") {
      warn = "请输入手机号码";
    } else if (!(/^1[0-9]{10}$/.test(e.detail.value.cellphone.trim()))) {
      warn = "手机号码格式不正确";
    } else {
      flag = false;
      console.log(this.data.addressId);
      console.log(app.globalData.openId);
      wx.request({
        url: 'https://cake.cindata.cn/address/addAddress',
        data: {
          openId: app.globalData.openId,
          addressId: this.data.addressId,
          firstName: e.detail.value.firstName,
          lastName: e.detail.value.lastName,
          address: e.detail.value.address,
          city: e.detail.value.city,
          zip: e.detail.value.zip,
          email: e.detail.value.email,
          wechatNumber: e.detail.value.wechatNumber,
          cellphone: e.detail.value.cellphone
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        complete: function complete(res) {
          if (res.statusCode == 200) {
            wx.navigateBack();
          } else {
            console.log("添加收货地址失败，请稍后重试！");
          }
        }
      });
    }  

    //如果信息填写不完整，弹出输入框  
    if (flag == true) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: warn
      })
    }
  },
  formReset: function (e) {
    this.setData({
      chosen: ''
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
})